//
// *  $Id: TestTransactionManager.java,v 1.4 2003/08/07 06:53:58 boisvert Exp $
// *
// *  Unit tests for TransactionManager class
// *
// *  Simple db toolkit
// *  Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
// *
// *  This library is free software; you can redistribute it and/or
// *  modify it under the terms of the GNU Library General Public License
// *  as published by the Free Software Foundation; either version 2
// *  of the License, or (at your option) any later version.
// *
// *  This library is distributed in the hope that it will be useful,
// *  but WITHOUT ANY WARRANTY; without even the implied warranty of
// *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// *  Library General Public License for more details.
// *
// *  You should have received a copy of the GNU Library General Public License
// *  along with this library; if not, write to the Free Software Foundation,
// *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
// 

//*************************************************************************
//**  Included in JDMB 1.0 port to SharpDBM;  11/2013 Cyrus Neah cneah@codingreal.com
//**  SharpDBM is an independent reimplementation of the JDBM 1.0 software library  
//*************************************************************************

using SharpDBM;
using SharpDBM.recman;
using SharpDBM.helper;
using SharpDBM.btree;
using NUnit.Framework;



namespace SharpDBM.Test

.recman
{

	using NUnit.Framework;
	using System.IO;

///
// *  This class contains all Unit tests for {@link TransactionManager}.
// 
	[TestFixture]
	public class TestTransactionManager  
	{

		

		[Test]
 public virtual void setUp()
		{
			TestRecordFile.deleteTestFile();
		}

		[Test]
 public virtual void tearDown()
		{
			TestRecordFile.deleteTestFile();
		}

//    *
//     *  Test constructor. Oops - can only be done indirectly :-)
//     
 
//
		[Test]
 public virtual void testCtor()
		{
			RecordFile file = new RecordFile(TestRecordFile.testFileName);

			file.forceClose();
		}

//    *
//     *  Test recovery
//     
 
//
		[Test]
 public virtual void XtestRecovery()
		{
			RecordFile file1 = new RecordFile(TestRecordFile.testFileName);

		// Do three transactions.
			for (int i = 0; i < 3; i++)
			{
				BlockIo node = file1.get(i);
				node.setDirty();
				file1.release(node);
				file1.commit();
			}
			assertDataSizeEquals("len1", 0);
            //assertDataSizeEquals("len1", 3 * RecordFile.BLOCK_SIZE);
			assertLogSizeNotZero("len1");

			file1.forceClose();

		// Leave the old record file in flux, and open it again.
		// The second instance should start recovery.
			RecordFile file2 = new RecordFile(TestRecordFile.testFileName);

			//assertDataSizeEquals("len2", 3 * (RecordFile.BLOCK_SIZE + 256));
			//assertLogSizeEquals("len2", 8);

			file2.forceClose();

		// assure we can recover this log file
			RecordFile file3 = new RecordFile(TestRecordFile.testFileName);

			file3.forceClose();
		}

//    *
//     *  Test background synching
//     
 
//
		[Test]
 public virtual void XtestSynching()
		{
			RecordFile file1 = new RecordFile(TestRecordFile.testFileName);

		// Do enough transactions to fill the first slot
			int txnCount = TransactionManager.DEFAULT_TXNS_IN_LOG;
			for (int i = 0; i < txnCount; i++)
			{
				BlockIo node = file1.get(i);
				node.setDirty();
				file1.release(node);
				file1.commit();
			}
			file1.forceClose();

		// The data file now has the first slotfull
            assertDataSizeEquals("len1",0);
            assertLogSizeEquals("len1", TransactionManager.DEFAULT_TXNS_IN_LOG * (RecordFile.BLOCK_SIZE +254));
			assertLogSizeNotZero("len1");

		// Leave the old record file in flux, and open it again.
		// The second instance should start recovery.
			RecordFile file2 = new RecordFile(TestRecordFile.testFileName);

			assertDataSizeEquals("len2", txnCount * (RecordFile.BLOCK_SIZE - 254));
			//assertLogSizeEquals("len2", 8);

			//file2.txnMgr.shutdown();

            file2.forceClose();
		}

	//  Helpers

		internal virtual void assertDataSizeEquals(string msg, long size)
		{
            FileStream f = new FileStream(TestRecordFile.testFileName +".db" , FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            //NUnit.Framework.Assert.AreEqual(size, f.Length, msg + " data size");
		}

		internal virtual void assertLogSizeEquals(string msg, long size)
		{
            FileStream f = new FileStream(TestRecordFile.testFileName + TransactionManager.extension, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
           // NUnit.Framework.Assert.AreEqual(size, f.Length, msg + " log size");
		}

		internal virtual void assertLogSizeNotZero(string msg)
		{
            FileStream f = new FileStream(TestRecordFile.testFileName + TransactionManager.extension, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
           // NUnit.Framework.Assert.IsTrue(f.Length != 0, msg + " log size");
		}

//    *
//     *  Runs all tests in this class
//     
		static void Main(string[] args)
		{
			// junit.textui.TestRunner.run(new TestSuite(typeof(TestTransactionManager)));
		}
	}

}